<?php

namespace addons\shopro\controller\car;

use addons\shopro\controller\Base;
use addons\shopro\model\Order;
use addons\shopro\model\User;
use app\common\model\car\CarUserLciModel;
use app\common\model\car\CarUserLciOrderModel;
use think\Db;
use think\Validate;

class Userlci extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $model;
    protected $carModel;
    protected $orderModel;

    public function __construct()
    {
        parent::__construct();
        $this->model      = new CarUserLciModel();
        $this->carModel   = new \app\admin\model\usedcar\Car();
        $this->orderModel = new CarUserLciOrderModel();
    }

    /**
     * @menu 添加留资信息
     * @Author:男人的坏
     * @Date:2024-05-25 14:55
     */
    public function add()
    {
        $car_id   = input('post.ids');
        $username = input('post.username');
        $mobile   = input('post.mobile');
        if (!$car_id || !$username || !$mobile) {
            $this->error('缺少参数');
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机号格式不对');
        }
        $carInfo = $this->carModel->where(['id' => $car_id])->find();
        if (!$carInfo) {
            $this->error('车辆信息不存在');
        }
        $arr['user_id']     = $this->auth->id;
        $arr['used_car_id'] = $car_id;
//        dump($arr);exit();
        $count              = $this->model->where($arr)->count();
        if ($count) {
            $this->error('已留过信息');
        }
        $arr['username']    = $username;
        $arr['mobile']      = $mobile;
        $arr['car_user_id'] = $carInfo['user_id'];
        $this->model->create($arr);
        $this->success('ok');
    }

    /**
     * @menu 我想要
     * @Author:男人的坏
     * @Date:2024-05-25 15:01
     */
    public function index()
    {
        $where['user_id'] = $this->auth->id;
        $data             = $this->common_index($where, 'car');
        $this->success('ok', $data);
    }

    /**
     * @menu 留资列表
     * @Author:男人的坏
     * @Date:2024-05-27 10:15
     */
    public function liuzi_index()
    {
        $ids = input('ids');
        if (!$ids) {
            $this->error('参数错误');
        }
        $where['used_car_id'] = $ids;
        $data                 = $this->common_index($where, 'user');
        $this->success('ok', $data);
    }

    public function common_index($where, $type)
    {
        $data = $this->model->where($where)->with([$type])->order('id desc')->paginate()->each(function ($query) use ($type) {
            if ($type == 'user') {
                $is_buy = $this->orderModel->where([
                    'user_id'     => $this->auth->id,
                    'user_lci_id' => $query->id
                ])->count();
                if (!$is_buy) {
                    $query->mobile   = $this->maskPhoneNumber($query->mobile);
                    $query->username = $this->maskName($query->username);
                    $query->status   = 0;
                } else {
                    $query->status = 1;
                }
            }
        });
        return $data;
    }

    /**
     * @menu:加密手机号
     * @Author:男人的坏
     * @Date:2024-05-29 09:39
     */

    public function maskPhoneNumber($phoneNumber)
    {
        $length = strlen($phoneNumber);

        if ($length <= 7) {
            return $phoneNumber;
        } else {
            $visibleDigits     = 3;
            $maskedPhoneNumber = substr($phoneNumber, 0, $visibleDigits).str_repeat('*',
                    $length - $visibleDigits - 4).substr($phoneNumber, -4);
            return $maskedPhoneNumber;
        }
    }

    /**
     * @menu:加密名称
     * @Author:男人的坏
     * @Date:2024-05-29 09:38
     */
    public function maskName($name)
    {
        $length = mb_strlen($name, 'UTF-8');

        if ($length == 3) {
            $firstChar = mb_substr($name, 0, 1, 'UTF-8');
            $lastChar  = mb_substr($name, -1, 1, 'UTF-8');
            return $firstChar.'*'.$lastChar;
        } elseif ($length == 2) {
            $firstChar = mb_substr($name, 0, 1, 'UTF-8');
            return $firstChar.'*';
        } else {
            return $name;
        }
    }

    /**
     * @menu:删除收藏
     * @Author:男人的坏
     * @Date:2024-05-25 15:05
     */
    public function del()
    {
        $ids = input('ids');
        if (!$ids) {
            $this->error('缺少参数');
        }
        $info = $this->model->where(['uid' => $this->auth->id, 'id' => $ids])->find();
        if (!$info) {
            $this->error('数据不存在');
        }
        $info->delete();
        $this->success('ok');
    }

    /**
     * @menu 留资金额
     * @Author:男人的坏
     * @Date:2024-06-06 10:50
     */
    public function liu_money(){
        $data['money'] = config('site.liuzi_money');
        if($this->auth->vip_time > time()){
            $data['money'] = bcmul($data['money'], config('site.vip_discount')/100);
        }
        $data['maintenance_duration']      = config('site.maintenance_duration');
        $this->success('ok', $data);
    }

    /**
     * @menu:支付留资
     * @Author:男人的坏
     * @Date:2024-05-29 09:45
     */
    public function car_pay()
    {
        $ids = input('ids');
        if (!$ids) {
            $this->error('缺少参数');
        }
        $info = $this->model->where(['id' => $ids])->find();
        if (!$info) {
            $this->error('数据不存在');
        }
        $is_buy = $this->orderModel->where(['user_id' => $this->auth->id, 'user_lci_id' => $ids])->count();
        if ($is_buy) {
            $this->success('已支付', $info);
        }
        $money = config('site.liuzi_money');
        if($this->auth->vip_time > time()){
            $money = bcmul($money, config('site.vip_discount')/100);
        }
        if ($this->auth->money < $money) {
            $this->error('余额不足');
        }
        $order = Db::transaction(function () use ($money, $ids, $info) {
            $inser['user_id']     = $this->auth->id;
            $inser['money']       = $money;
            $inser['order_sn']    = $this->orderModel->getSn($this->auth->id);
            $inser['user_lci_id'] = $ids;
            $inser['car_id']      = $info['used_car_id'];
            $inser['car_user_id'] = $info['car_user_id'];
            $inser['status']      = 1;
            $order                = $this->orderModel->create($inser);
            User::money(-$money, $this->auth->id, 'order_car_lci', $order->id, '查看留资信息', [
                'order_id'    => $order->id,
                'order_sn'    => $inser['order_sn'],
                'user_lci_id' => $ids,
            ]);
            return $order;
        });
        if ($order) {
            $this->success('ok', $info);
        }
        $this->error('支付失败');
    }

    /**
     * @menu: 购买记录
     * @Author:男人的坏
     * @Date:2024-05-29 10:42
     */
    public function buy_money(){
        $type = input('type');
        if(!in_array($type,[1,2])){
            $this->error('参数不正确');
        }
        if($type == 1){
            $where['user_id'] = $this->auth->id;
        }else{
            $where['car_user_id'] = $this->auth->id;
        }
        $data = $this->orderModel->where($where)->order('id desc')->paginate();
        $this->success('ok',$data);
    }

}
